package com.sanjiahantang.ythshopping.adapter.home;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sanjiahantang.ythshopping.R;
import com.sanjiahantang.ythshopping.activity.MainActivity;
import com.sanjiahantang.ythshopping.activity.me.JsWebActivity;
import com.sanjiahantang.ythshopping.config.ConfigValue;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;

import java.io.IOException;
import java.net.URLDecoder;

import static com.squareup.picasso.Picasso.with;

/**
 * Created by Administrator on 2017/6/8.
 */

public class ImageHolder extends MyBaseViewHolder {

    private Context context;
    private View view;
    private String src;
    private String url;

    public ImageHolder(View itemView) {
        super(itemView);
    }

    public ImageHolder(Context context, View itemView, String src,String url) {
        super(itemView);
        this.context = context;
        this.src = src;
        this.view = itemView;
        this.url = url;
    }

    @Override
    void bindData() {
        final ImageView iv = (ImageView) view.findViewById(R.id.iv);
        iv.setVisibility(View.GONE);
        Picasso.with(context).load(ConfigValue.IMG_IP + src).into(iv, new Callback() {
            @Override
            public void onSuccess() {
                iv.setVisibility(View.VISIBLE);
            }

            @Override
            public void onError() {
                iv.setVisibility(View.GONE);
            }
        });
        LinearLayout.LayoutParams params = new LinearLayout
                .LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(params);

        if(!TextUtils.isEmpty(url) && (url.toLowerCase().contains("http://") || url.toLowerCase().contains("https://"))){
            iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intentjs = new Intent(context, JsWebActivity.class);
                    intentjs.putExtra("url", url);
                    context.startActivity(intentjs);
                }
            });
        }
    }
}
